package function;

import bean.Student;

import java.util.UUID;
import java.util.function.*;

/**
 * 四大内置函数式接口
 * @Author: lyy
 * Date: 2021/12/23 9:54
 */
public class FunctionTest {
	public static void main(String[] args) {
		// Function：有入参，有返回值
		String result = function("a----", str -> {
			String s1 = str+ UUID.randomUUID().toString();
			s1 += "---end";
			return s1;
		});
		System.out.println(result);

		// BiFunction：与Function的区别就是多一个入参
		Integer biResult = biFunction(3, 4, (a, b) -> a * b);
		System.out.println(biResult);

		// Consumer：有入参，无返回值
		consumer("发送天气预报", str -> {
			System.out.println("开始发送");
			System.out.println("本地发送内容" + str);
		});

		// Supplier：无入参，有返回值
		String name = supplier(() -> Student.build());
		System.out.println(name);

		// Predicate：有入参，返回boolean类型
		boolean type = predicate(2, t -> t == 2);
		boolean type1 = predicate(2, t -> t == 2);
		System.out.println(type + "---" + type1);
	}

	public static String function(String s, Function<String, String> function){
		return function.apply(s);
	}

	public static Integer biFunction(Integer a, Integer b, BiFunction<Integer, Integer, Integer> biFunction){
		return biFunction.apply(a, b);
	}

	public static void consumer(String message, Consumer<String> consumer){
		consumer.accept(message);
	}

	public static String supplier(Supplier<Student> supplier){
		return supplier.get().getName();
	}

	public static boolean predicate(Integer type, Predicate<Integer> predicate){
		return predicate.test(type);
	}
}


